package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
@Slf4j //日志
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        // 根据用户名查询密码
        Employee employee =   employeeMapper.findEmployeeByUsername(username);
//        log.info("密码明文:{}",employee.getPassword());
        // 需要把密码进行加密，变成密文
        // 登录加密之后，需要把数据库的密码也改成密文
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        log.info("密码密文:{}",password);

        // 判断查询出来的用户是否存在
        if (null == employee){
            throw new  AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //如果代码能够到这个地方说明账号存在，就需要判断密码
        if (!employee.getPassword().equals(password)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        //状态 0:禁用，1:启用
        // 判断当前用户是否可用
        if (employee.getStatus() == StatusConstant.DISABLE){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        return employee;
    }

    @Override
    public void updatePassword(PasswordEditDTO passwordEditDTO) {
        // 因为需要把旧密码改成新密码，所以需要获取旧密码和新密码
        String oldPassword = passwordEditDTO.getOldPassword();
        String newPassword = passwordEditDTO.getNewPassword();
        //获取当前登录用户的id
        Long currentId = BaseContext.getCurrentId();
        log.info("修改密码：emid={}",currentId);
        // 对原密码进行加密
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());

        //根据当前登录用户的id进行查询用户对象
        Employee employee =  employeeMapper.findEmployeeByIdAndPassword(oldPassword,currentId);
        //如果查询到当前登录用户的对象，就可以进行修改密码
        if (employee!=null){
            //更新密码
            employeeMapper.updatePassword(oldPassword,newPassword,currentId);
        }else {
            //如果没有查询到对象，说明旧密码输入有误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //EmployeeDTO前端传递过来的数据，需要保存到bean对象里面丢到数据库
        //参数一：原对象
        //参数二：目标对象，
        //把原对象里面的数据复制到目标对象
        BeanUtils.copyProperties(employeeDTO,employee);
        //设置账号的状态 状态 0:禁用，1:启用
        employee.setStatus(StatusConstant.ENABLE);
        //设置密码
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        //设置创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置用户id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());

            //插入数据,在插入数据之前，先查询是否有当前登录的用户数据，如果有，就不允许插入数据
        employeeMapper.insert(employee);



    }


}
